<?php
namespace User\Controller;

class ProfileController extends MainController
{
    public function index()
    {
        $account = M('Member')->where('userid=' . $this->userID)->find();
        $this->assign('account', $account);
        $state = M('States')->select();
        $this->assign('state', $state);
        $nowYear = date('Y') - 10;
        $this->assign('nowYear', $nowYear);
        $this->display();
    }
    public function save()
    {
        $obj = M('User');
        $map['userid'] = $this->userID;
        $list = $obj->where($map)->find();
        $data['firstname'] = I('post.firstname');
        $data['lastname'] = I('post.lastname');
        $data['nickname'] = I('post.nickname');
        $data['gender'] = I('post.gender');
        $data['birthday'] = I('post.birthday');
        $data['address'] = I('post.address');
        $data['address2'] = I('post.address2');
        $data['phone'] = I('post.phone');
        $data['city'] = I('post.city');
        $data['state'] = I('post.state');
        $data['zip'] = I('post.zip');
        $data['school'] = I('post.school');
        $data['major'] = I('post.major');
        $data['desired'] = I('post.desired');
        $data['description'] = I('post.description');
        $data['updateTime'] = time();
        if ($list) {
            $obj->where($map)->save($data);
        } else {
            $data['userid'] = $this->userID;
            $data['createTime'] = time();
            $obj->add($data);
        }
        $state = 'SUCCESS';
        $message = L('_action_success');
        $url = U('Index/index');
        echo $this->echo_json_str($state, $message, $url);
    }

    //编辑密码
    public function setpsd()
    {
        if ($_POST) {
            $oldPwd = I("oldpassword");
            $password = I("password");
            $map['userid'] = $this->userID;
            $list = M("Member")->where($map)->find();
            if ($list['password'] != md5($oldPwd)) {
                echo $this->echo_json_str("Old password input error");
                exit;
            } else {
                if (M("Member")->where($map)->setField("password", md5($password))) {
                    cookie(null);
                    echo $this->echo_json_str("SUCCESS", L("_action_success"), U("Home/Login/index"));
                    exit;
                } else {
                    echo $this->echo_json_str("Your new password can't be your old password");
                    exit;
                }
            }
        } else {
            $this->display();
        }
    }
}